﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base.Format;
using UFIDA.U9.Base.Language;
using UFIDA.U9.Base.PropertyTypes;
using UFIDA.U9.Base.UserRole;
using UFSoft.UBF.Business;
using static System.Net.Mime.MediaTypeNames;

namespace ufida.u9.ait.openapi
{
    internal class UserBase
    {
        /// <summary>
        /// 修改用户密码
        /// 参考BP changePassWordBP
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password">新密码</param>
        /// <returns></returns>
        public static User ChangePassword(User user,string password)
        {
            using(ISession session = Session.Open())
            {
                session.Modify(user);

                //加密
                EncryptBP encryptBP = new EncryptBP();
                encryptBP.Source = password;
                string text2 = encryptBP.Do();
                string text3 = EncryptionManager.Instance.EncryptForSalt(text2 + user.Salt);
                user.Password = text3;

                //用户密码历史
                //UserPassWordHistory userPassWordHistory2 = UserPassWordHistory.Create();
                //userPassWordHistory2.User = user;
                //userPassWordHistory2.PassWord = text3;

                session.Commit();
            }
            return user;
        }
        public static User CreateDoc(UserAITDTOData dto)
        {
            User user;
            using(ISession session = Session.Open())
            {
                user = User.Create();
                user.Code = dto.Code;
                user.Name=dto.Name;
                user.DisplayName=dto.DisplayName;
                user.TipAnswer = dto.Code;
                user.Tip = dto.Code;
                user.Style = UserTypeEnum.Staffer;
                user.MaxUserCount = 1;
                user.LoginLanguage = EntityUtils.GetEntityByCode<Language>("zh-CN");
                user.IsAlive = true;
                user.DateFormat = DateFormat.Finder.Find("1=1");

                //有效性
                user.Effective = new Effective()
                {
                    IsEffective = true,//生效标志
                    EffectiveDate = DateTime.Now,
                    DisableDate = DateTime.MaxValue,
                };
                if (dto.Effective != null)
                {
                    user.Effective.IsEffective = dto.Effective.IsEffective;
                    if (!string.IsNullOrEmpty(dto.Effective.EffectiveDate))
                    {
                        user.Effective.EffectiveDate=DateTime.Parse(dto.Effective.EffectiveDate);
                    }
                    if (!string.IsNullOrEmpty(dto.Effective.DisableDate))
                    {
                        user.Effective.DisableDate = DateTime.Parse(dto.Effective.DisableDate);
                    }
                }

                session.Commit();
            }

            if (!string.IsNullOrEmpty(dto.Password))
            {
                //传入密码非空
                //修改用户密码
                ChangePassword(user, dto.Password);
            }
            
            return user;
        }

        public static User UpdateDoc(UserAITDTOData dto,User user)
        {
            using (ISession session = Session.Open())
            {
                user.Name = dto.Name;
                user.DisplayName = dto.DisplayName;

                //有效性
                if (dto.Effective != null)
                {
                    user.Effective.IsEffective = dto.Effective.IsEffective;
                    if (!string.IsNullOrEmpty(dto.Effective.EffectiveDate))
                    {
                        user.Effective.EffectiveDate = DateTime.Parse(dto.Effective.EffectiveDate);
                    }
                    if (!string.IsNullOrEmpty(dto.Effective.DisableDate))
                    {
                        user.Effective.DisableDate = DateTime.Parse(dto.Effective.DisableDate);
                    }
                }

                session.Commit();
            }

            if (!string.IsNullOrEmpty(dto.Password))
            {
                //传入密码非空
                //修改用户密码
                ChangePassword(user, dto.Password);
            }

            return user;
        }
    }
}
